/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.survey;

import main.CommonMethods;
import exceptions.IntelParsingException;

/**
 * This class parses surveys
 * 
 * 
 */
public class SurveyParser {
   private final String buildingsregex = CommonMethods.getBuildingsRegex();

   public void parseFormattedSurvey(final String input, final Survey survey) throws IntelParsingException {
	  String in = input;
	  if (in.matches("Buildings Report of .*\\([0-9]{1,2}(:)[0-9]{1,2}\\)")) {
		 in = in.replace("Buildings Report of ", "");

		 String prov = in.substring(0, in.indexOf('(')).trim();
		 String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 survey.setName(prov);
		 survey.setKdloc(kd);
	  } else if (in.matches("[0-9]{1,2}\\. " + buildingsregex + ": [0-9,]{1,} \\([0-9]{1,3}[.]{0,1}[0-9]{0,2}%\\)")) {
		 in = in.substring(in.indexOf(' ')).trim();
		 String type = in.substring(0, in.indexOf(':'));
		 type = CommonMethods.getBuildingsMapping(type);
		 in = in.replace(type + ": ", "");
		 String building = in.substring(0, in.indexOf(' ')).trim();
		 survey.setBuilding(type, Integer.parseInt(building.replace(",", "")));
	  } else if (in.matches("[0-9]{1,2}\\. " + buildingsregex + ": [0-9,]{1,} \\([0-9]{1,3}[.]{0,1}[0-9]{0,2}%\\) "
			+ "\\+ [0-9,]{1,} in progress \\(\\+[0-9]{1,3}[.]{0,1}[0-9]{0,2}%\\)")) {
		 in = in.substring(in.indexOf(' ')).trim();
		 String type = in.substring(0, in.indexOf(':'));
		 type = CommonMethods.getBuildingsMapping(type);
		 in = in.replace(type + ": ", "");
		 String building = in.substring(0, in.indexOf(' ')).trim();
		 survey.setBuilding(type, Integer.parseInt(building.replace(",", "")));
		 String inprog = in.substring(in.indexOf('+') + 2);
		 inprog = inprog.substring(0, inprog.indexOf(' '));
		 survey.setBuildingInProgress(type, Integer.parseInt(inprog.replace(",", "")));
	  } else if (in.matches("Total Land: [0-9,]{1,} Acre.*") && !"".equals(survey.getKdloc())) { // kdloc empty if self survey
		 String total = in.substring(12, in.indexOf("Acre")).replaceAll("[^0-9]", "");
		 survey.setBuilding("Barren Land", Integer.parseInt(total) - survey.getTotalbuilt());
	  } else if (in.matches("Total Land: [0-9,]{1,} Acre.*") && "".equals(survey.getKdloc())) {
		 String total = in.substring(12, in.indexOf("Acre")).replaceAll("[^0-9]", "");
		 survey.setSelfSurvey(true);
		 survey.setBuilding("Barren Land", Integer.parseInt(total) - survey.getTotalbuilt() - survey.getTotalprogress());
	  }
   }

   public boolean parseUnformattedSurvey(final String input, final Survey survey) throws IntelParsingException {
	  final int size = survey.getIntel().size();
	  final String buildingsregex = CommonMethods.getBuildingsRegex();
	  String in = input;
	  if (in.matches("Our thieves scour the lands of .*\\([0-9]{1,2}(:)[0-9]{1,2}\\).*")) {
		 in = in.replace("Our thieves scour the lands of ", "");

		 final String prov = in.substring(0, in.indexOf('(')).trim();
		 final String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 survey.setName(prov);
		 survey.setKdloc(kd);
	  } else if (in.matches(buildingsregex + "\t[0-9,]{1,}.*")) {
		 final String[] temp = in.split("\t");
		 survey.setBuilding(temp[0].trim(), Integer.parseInt(temp[1].replace(",", "").trim()));
	  }
	  return size < survey.getIntel().size();
   }
}
